import React, {Component} from 'react'
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import {WeaTop, WeaLeftRightLayout, WeaNewScroll,WeaBrowser} from 'ecCom'
import { Base64 } from 'js-base64';
import {Button, Modal} from 'antd'

const confirm = Modal.confirm;

import LeftContainer from './LeftContainer/index'
import RightContainer from './RightContainer/index'

import GroupSelect from "./RightContainer/Attention/GroupSelect"
import '../../style/myBlog/myBlog.less'

const P = 'wea-myBlog'

import WeaUtils from "../Component/wea-utils"

const {DateFormat, DateAddition} = WeaUtils

import {WeaLocaleProvider} from "ecCom";

const getLabel = WeaLocaleProvider.getLabel;

@inject('blogMyBlog')
@observer
class MyBlog extends Component {

  constructor(props) {
    super(props)
   const {cardType = 2} = this.props.params;
    const {weiboType = 1} = this.props.params;
    this.state = {
      topHeight: 0,
      cardType: cardType,
      weiboType: weiboType,
      attentionCheck: false,
      currentGroup: "all",
      isReset: false,
      showLeft: true,
      selectedKey:"-1"
    }
  }

  componentDidMount() {
      let url = window.location.href;
      let wbtype = this.state.cardType*10+this.state.weiboType*1;
      const {blogMyBlog} = this.props;
      let pms = {currentPage: 1, type: "reply"};
      if(url.indexOf("/comment")>0){
          wbtype = 22;
      }else if(url.indexOf("/Zan")>0){
          wbtype = 22;
          pms ={currentPage: 1, type: "zan"};
      }else if(url.indexOf("/atMe")>0){
          wbtype = 22;
          pms = {currentPage: 1, type: "at"};
      }

      if(window.location.href.indexOf("/homepage")>0){
          blogMyBlog.getUserInfo();
         this.selectCard(1);
      }else{
          blogMyBlog.init(wbtype,pms);
      }



  }



  testOnclck = () =>{

      const {batchAttention} = this.props.blogMyBlog;

  }

  batchAttention = (ids) => {
      const {batchAttention} = this.props.blogMyBlog;
      if(ids==''){

      }else{
          batchAttention({ids:ids});
      }


  }

  getYGZIDS = () => {
 const {getFilterIds} = this.props.blogMyBlog;
   getFilterIds();
  }

  render() {
    const {
        loading,gzbrowsercondition, title, userInfo, weiboCKEditorConfig, indexInfo, basicInfo, receiverList,
        visitorList, weiboList, systemWorkLog, weiboGroup, fanList, attentionList,
        messageList, note, defaultTemplate, keyWords,bacthAttentionDisabled
      } = toJS(this.props.blogMyBlog.status),
      {cardType, weiboType, topHeight, attentionCheck, currentGroup,selectedKey} = this.state;
    let groups = weiboGroup.groupList || []

      let buttonList =[];

    if(userInfo.currentUser){
        if(userInfo.currentUser.allowRequest=='1'&&!this.props.blogMyBlog.isSpecial) {
            buttonList.push( {
                name: getLabel('26939',"添加关注"),
                disabled: false,
                cancelSelect: true,
            });
        }
    }



    buttonList.push(  {
        name: getLabel(81296, "移动到组"),
        disabled: !attentionCheck,
        title: getLabel(31194, "新建分组并移动"),
        disabledGroup: currentGroup,
        onChange: this.selectGroup.bind(null, "move"),
        createGroup: this.selectGroup.bind(null, "createAndMove"),
        actionName:"createAndMove"
    })

      buttonList.push(  {
          name: getLabel(31267, "复制到组"),
          disabled: !attentionCheck,
          title: getLabel(31195, "新建分组并复制"),
          disabledGroup: currentGroup,
          onChange: this.selectGroup.bind(null, "copy"),
          createGroup: this.selectGroup.bind(null, "createAndCopy"),
          actionName:"createAndCopy"
      })

      if(this.props.blogMyBlog.isSpecial){
          buttonList.push({
              name:getLabel(514283,'取消特别关注') ,
              disabled:  (!attentionCheck),
              cancelSelect: true,
              onClick:this.cancelSpecialAtt.bind()
          })
      }else{
          buttonList.push({
              name: getLabel(83150, "从该组删除"),
              disabled: currentGroup == "all" || currentGroup == "nogroup" || (currentGroup != "all" && currentGroup != "nogroup" && !attentionCheck),
              cancelSelect: true,
              onClick: this.selectGroup.bind(null, "remove")
          })
      }


      buttonList.push({
          name: getLabel(26938, "取消关注"),
          disabled: !attentionCheck,
          cancelSelect: true,
          onClick: this.cancelAttention
      })



    let groupList = []
    groups.forEach(
      item => {
        if (item.groupId != "all" && item.groupId != "nogroup") {
          groupList.push(item)
        }
      }
    )

    let renderContainer = () => (
      <WeaLeftRightLayout ecId={`${this && this.props && this.props.ecId || ''}_WeaLeftRightLayout@zalsvp`}
        isNew={true}
        showLeft={this.state.showLeft}
        onCollapse={v => {
          this.setState({showLeft: v})
        }}
        leftCom={
          <LeftContainer ecId={`${this && this.props && this.props.ecId || ''}_LeftContainer@mnd9md`}
            cardType={cardType}
            onClick={this.selectCard}
            userInfo={userInfo}
            indexInfo={indexInfo}
            basicInfo={basicInfo}
            messageList={messageList}
            receiverList={receiverList}
            visitorList={visitorList}
            weiboCKEditorConfig={weiboCKEditorConfig}
            note={note}
          />
        }
      >
        {<RightContainer ecId={`${this && this.props && this.props.ecId || ''}_RightContainer@gm9f8k`}
          ref="rightContainer"
          checkAttention={this.checkAttention}
          changeGroup={this.changeGroup}
          cardType={cardType}
          weiboType={weiboType}
          selectedKey={selectedKey}
          topHeight={topHeight}
          userInfo={userInfo}
          weiboGroup={weiboGroup}
          weiboList={weiboList}
          basicInfo={basicInfo}
          fanList={fanList}
          defaultTemplate={defaultTemplate}
          attentionList={attentionList}
          systemWorkLog={systemWorkLog}
          weiboCKEditorConfig={weiboCKEditorConfig}
          changeReset={this.changeReset}
          keyWords={keyWords}
          loading={loading}
          selectGroup={this.selectGroup}
          cancelAttentionSelect={this.cancelAttentionSelect}
        />}
      </WeaLeftRightLayout>
    )

    return (
      <div className={P}>
        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@ni4fyu`}
          title={title}
          loading={loading}
          icon={<span className='icon-coms-blog'/>}
          iconBgcolor="#6d3cf7"
          buttonSpace={10}
          buttons={
            (cardType == 4 ? (
              buttonList.map(
                item => {
                  let {name, title, disabled, disabledGroup, cancelSelect, ...rest} = item

                  if(name==getLabel('26939',"添加关注")) {
                     return (
                         <div onClick={this.getYGZIDS}>
                         <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@yiljw7@browser1`}
                             ref="browser1"
                             type={17}
                             customized
                             title={getLabel('30042',"人员")}
                             inputStyle={{ width: 200 }}
                             onChange={(ids, names, datas) => {
                                this.batchAttention(ids);
                             }
                             }
                             dataParams={{
                                 filterids:gzbrowsercondition
                             }}
                             completeParams={{
                                 filterids:gzbrowsercondition
                             }}
                             isSingle={false}
                         >
                             <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3wml0s@4`} type="primary">{getLabel('26939',"添加关注")}</Button>
                         </WeaBrowser>
                         </div>
                     )
                  }
                  if (cancelSelect) {
                    return (
                      <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@6l0kgi@3`}
                        type="primary"
                        disabled={disabled} {...rest}
                      >{name}</Button>
                    )
                  } else {
                    return (
                      <GroupSelect ecId={`${this && this.props && this.props.ecId || ''}_GroupSelect@edhbs1@2`}
                        disabledGroup={disabledGroup}
                        onCheck={this.checkAttention}
                        groups={groupList}
                        title={title}
                        {...rest}
                      >
                        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@c0b455@1`} type="primary" disabled={disabled}>{name}</Button>
                      </GroupSelect>
                    )
                  }
                }
              )
            ) : (cardType == 2 ?  [
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@u0ifsr@yBlogExport`} type="primary" onClick={() => {
                this.props.blogMyBlog.myBlogExport();
              }}>{getLabel(28343, "导出Excel")}</Button>,
            ]:cardType == 5 ?[
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@w94qxt@oMeAttentio`}  disabled={!bacthAttentionDisabled} type="primary" onClick={() => {
                this.props.blogMyBlog.batchShareToMeAttention(result => {
                  if (result.status == "1") {
                     this.cancelAttentionSelect()
                  }
              });
              }}>{getLabel(503955, "批量关注")}</Button>,
            ]:[] )
            )
          }
          showDropIcon={false}
          getHeight={this.getTopHeight}
        >
          {renderContainer()}
        </WeaTop>
      </div>
    )
  }

  changeReset = (value) => {
    this.setState({isReset: value})
  }

  getTopHeight = (height) => {
    this.setState({topHeight: height})
  }



  selectCard = (currentType) => {
    const {getFanList, getAttentionList, getWeiboGroup,searchAdForShareTable,getShareToMeUserList} = this.props.blogMyBlog
    let params;
    switch (currentType) {
      case 1 :
          const { form } = searchAdForShareTable;
          let params =
            (searchAdForShareTable.form &&
              searchAdForShareTable.form.getFormParams()) ||
            {};
            params["currentPage"] = 1
            params["groupId"] = -1
        //params = {currentPage: 1, groupId: "all"};
        this.refs.rightContainer.wrappedInstance.getWeiboListByAction({
          type: currentType * 10 + 1,
          status: true
        }, params)
        break;
      case 2 :
        params = {endDate: DateFormat(new Date(), "yyyy-MM-dd")};
        this.refs.rightContainer.wrappedInstance.getWeiboListByAction({
          type: currentType * 10 + 1,
          status: true
        }, params)
        break;
      case 3 :
        getFanList()
        break;
      case 4 :
        getWeiboGroup()
        getAttentionList({groupId: "all"})
        break;
      case 5 :
        getShareToMeUserList({groupId: "all"})
      default:
        break;
    }
    // getBasicInfo();//为了刷新左侧的微博主页个数
    this.setState({cardType: currentType})
    this.refs.rightContainer.wrappedInstance.clear = true
    //}
  }

  checkAttention = (value) => {
    this.setState({attentionCheck: value})
  }

  changeGroup = (value) => {
    this.setState({currentGroup: value.groupId})
  }

  cancelAttention = () => {
    let _executeCancelAttention = this.executeCancelAttention.bind(this);
    confirm({
      title: getLabel('384420', "确认要取消关注吗？"),
      content: '',
      onOk() {
        _executeCancelAttention();
      },
      onCancel() {
      },
    });
  }

  executeCancelAttention = () => {
    let {editAttitions, getAttentionList, getBasicInfo, getWeiboGroup,isSpecial} = this.props.blogMyBlog,
      {currentGroup} = this.state,
      // ref = this.refs.rightContainer.refs.attentions
      ref = ''
    if (this.refs.rightContainer.wrappedInstance.refs.attentions.wrappedInstance) {
      ref = this.refs.rightContainer.wrappedInstance.refs.attentions.wrappedInstance
    } else {
      ref = this.refs.rightContainer.wrappedInstance.refs.attentions
    }



    editAttitions(
      {idSet: ref.userIdsString, islowers: ref.isLowersString,},
      result => {
        if (result.status == "1") {

            if(isSpecial){
                getAttentionList({groupId:'special'})
            }else{
                getAttentionList({groupId:currentGroup})
                getWeiboGroup()
            }
            getBasicInfo()
            ref.clearData = true

        }
      }
    )
  }
  cancelAttentionSelect = () => {
    this.refs.rightContainer.wrappedInstance.refs.shareToMes.wrappedInstance.clearData = true;
  }


    cancelSpecialAtt = () => {
        let  ref = this.refs.rightContainer.refs.attentions
        if (this.refs.rightContainer.wrappedInstance.refs.attentions.wrappedInstance) {
            ref = this.refs.rightContainer.wrappedInstance.refs.attentions.wrappedInstance
        } else {
            ref = this.refs.rightContainer.wrappedInstance.refs.attentions
        }

        let idSet = ref.userIdsString;
        const {cancelSpecialUsers} =  this.props.blogMyBlog;
        confirm({
            title: getLabel(514284,'确定要取消特别关注吗') ,
            content: '',
            onOk() {
               cancelSpecialUsers({"specids":idSet});
                ref.clearData = true
            },
            onCancel() {

            },
        });
    }

  selectGroup = (type, obj) => {
    let {copyGroup, getWeiboGroup, getAttentionList, moveGroup, removeGroup, createAndMoveGroup, createAndCopyGroup,isSpecial} = this.props.blogMyBlog;
    let {currentGroup} = this.state,
      ref = this.refs.rightContainer.refs.attentions
    if (this.refs.rightContainer.wrappedInstance.refs.attentions.wrappedInstance) {
      ref = this.refs.rightContainer.wrappedInstance.refs.attentions.wrappedInstance
    } else {
      ref = this.refs.rightContainer.wrappedInstance.refs.attentions
    }

    if(isSpecial){
        currentGroup = 'special';
    }

    let idSet = ref.userIdsString;
    switch (type) {
      case "copy":
        copyGroup(
          {idSet: idSet, destGroup: obj.value},
          result => {
            if (result.status == "1") {
              getWeiboGroup()
              getAttentionList({groupId: currentGroup})
              ref.clearData = true
            }
          }
        )
        break;
      case "move":
        moveGroup(
          {idSet: idSet, sourceGroup: currentGroup, destGroup: obj.value},
          result => {
            if (result.status == "1") {
              getWeiboGroup()
              getAttentionList({groupId: currentGroup})
              ref.clearData = true
            }
          }
        )
        break;
      case "remove":
        removeGroup(
          {idSet: idSet, sourceGroup: currentGroup},
          result => {
            if (result.status == "1") {
              getWeiboGroup()
              getAttentionList({groupId: currentGroup})
              ref.clearData = true
            }
          }
        )
        break;
      case "createAndMove":
        createAndMoveGroup(
          {idSet: idSet, sourceGroup: currentGroup, groupName: obj},
          result => {
            if (result.status == "1") {
              getWeiboGroup()
              getAttentionList({groupId: currentGroup})
              ref.clearData = true
            }
          }
        )
        break;
      case "createAndCopy":
        createAndCopyGroup(
          {idSet: idSet, groupName: obj},
          result => {
            if (result.status == "1") {
              getWeiboGroup()
              getAttentionList({groupId: currentGroup})
              ref.clearData = true
            }
          }
        )
        break;
      default:
        break;
    }
  }
}

export default MyBlog